@echo off

setlocal

set "RUNNING_TASKS_COUNTER_LOCK_FILE0=%~1"
set "RUNNING_TASKS_COUNTER_FILE0=%~2"

if not defined LAST_ERROR set LAST_ERROR=0

set /P "FILE_PATH_0="
set /P "FILE_PATH_1="

rem BUG WORKAROUND:
rem   The last backslash duplication is required to avoid the last quote character escaping in case of executable call (not a script call)
if "%FILE_PATH_0:~-1%" == "\" if not "%FILE_PATH_0:~-2,1%" == "\" set "FILE_PATH_0=%FILE_PATH_0%\"
if "%FILE_PATH_1:~-1%" == "\" if not "%FILE_PATH_1:~-2,1%" == "\" set "FILE_PATH_1=%FILE_PATH_1%\"

if %FLAG_ARAXIS%0 NEQ 0 goto ARAXIS_CONSOLE_COMPARE_TOOL
if %FLAG_WINMERGE%0 NEQ 0 goto WINMERGE_COMPARE_TOOL

:WINMERGE_COMPARE_TOOL
if defined WINMERGE_COMPARE_TOOL_FLAGS set WINMERGE_COMPARE_TOOL_FLAGS= %WINMERGE_COMPARE_TOOL_FLAGS%
call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/call.bat" start /B /WAIT "" "%%WINMERGE_COMPARE_TOOL%%"%%BARE_FLAGS%%%%WINMERGE_COMPARE_TOOL_FLAGS%% "%%FILE_PATH_0%%" "%%FILE_PATH_1%%"

if %ERRORLEVEL% NEQ 0 if %LAST_ERROR% EQU 0 set LAST_ERROR=%ERRORLEVEL%

call "%%CONTOOLS_ROOT%%/locks/decr_var_file.bat" 0 "%%RUNNING_TASKS_COUNTER_LOCK_FILE0%%" "%%RUNNING_TASKS_COUNTER_FILE0%%"

exit /b %LAST_ERROR%

:ARAXIS_CONSOLE_COMPARE_TOOL
if defined ARAXIS_CONSOLE_COMPARE_TOOL_FLAGS set ARAXIS_CONSOLE_COMPARE_TOOL_FLAGS= %ARAXIS_CONSOLE_COMPARE_TOOL_FLAGS%
call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/call.bat" start /B /WAIT "" "%%ARAXIS_CONSOLE_COMPARE_TOOL%%"%%BARE_FLAGS%%%%ARAXIS_CONSOLE_COMPARE_TOOL_FLAGS%% /wait "%%FILE_PATH_0%%" "%%FILE_PATH_1%%"
rem call "%%CONTOOLS_BUILD_TOOLS_ROOT%%/call.bat" start /B "" "%%ARAXIS_CONSOLE_COMPARE_TOOL%%"%%BARE_FLAGS%% /nowait "%%FILE_PATH_0%%" "%%FILE_PATH_1%%"

if %ERRORLEVEL% NEQ 0 if %LAST_ERROR% EQU 0 set LAST_ERROR=%ERRORLEVEL%

call "%%CONTOOLS_ROOT%%/locks/decr_var_file.bat" 0 "%%RUNNING_TASKS_COUNTER_LOCK_FILE0%%" "%%RUNNING_TASKS_COUNTER_FILE0%%"

if defined ARAXIS_BEFORE_RENEW_INSTANCE_TIMEOUT_MSEC call "%%CONTOOLS_ROOT%%/std/sleep.bat" %%ARAXIS_BEFORE_RENEW_INSTANCE_TIMEOUT_MSEC%%

exit /b %LAST_ERROR%
